package tt.love.hw;

import javax.jdo.PersistenceManager;

public class JDOClient {

	private Object execute(JDOClientCallback callback) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return callback.doInJDOClient(pm);
		} finally {
			pm.close();
		}
	}

	public Object store(final Object object) {
		return execute(new JDOClientCallback() {
			public Object doInJDOClient(PersistenceManager pm) {
				return pm.makePersistent(object);
			}

		});
	}

	public Object getObjectById(final Class<?> clazz, final Object key) {
		return execute(new JDOClientCallback() {
			public Object doInJDOClient(PersistenceManager pm) {
				return pm.getObjectById(clazz, key);
			}
		});
	}

	public void update(final Class<?> clazz, final Object key) {
		execute(new JDOClientCallback() {
			public Object doInJDOClient(PersistenceManager pm) {
				pm.getObjectById(clazz, key);
				return null;
			}
		});
	}

}
